﻿var refreshGirdData; // 更新数据

var companyId = '';
var ztId = '';
var kjnd = '';
var kjqj = '';
var kjkm = [];
var userdefault = {};
var alreadyformat = false;

var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            page.initGrid();
            page.bind();

        },
        bind: function () {
            $('#pzrqMonth').lrselect({
                type: 'default',
                // 展开最大高度
                maxHeight: 300,
                placeholder: '月份',
                data: [
                    {'id': '01', 'text': '01'},
                    {'id': '02', 'text': '02'},
                    {'id': '03', 'text': '03'},
                    {'id': '04', 'text': '04'},
                    {'id': '05', 'text': '05'},
                    {'id': '06', 'text': '06'},
                    {'id': '07', 'text': '07'},
                    {'id': '08', 'text': '08'},
                    {'id': '09', 'text': '09'},
                    {'id': '10', 'text': '10'},
                    {'id': '11', 'text': '11'},
                    {'id': '12', 'text': '12'}
                ],
                select: function (item) {
                    if (!item || item.id == '-1') {
                    } else {
                        kjqj = item.id;
                    }
                }
            }).lrselectSetFirst();
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'zts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;
                                    $("#zt_title").html(ztItem.text);
                                    $('#kjnd_select').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'kjnds/dropdownlist?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: companyId, ztId: ztItem.value},
                                    }).lrselectSet(userdefault.kjnd);
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 会计年度选择
            $('#kjnd_select').lrselect({
                type: 'default',
                //placeholder: '请选择会计年度',
                // 展开最大高度
                maxHeight: 300,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);

                        $('#kjkmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'cnkms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                                {
                                    label: "余额方向", name: "yefx", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "借" : "贷";
                                    }
                                },
                                {
                                    label: "项目", name: "isXmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "部门", name: "isBmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "案件", name: "isAjHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "票据号", name: "isPjhHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "支票号", name: "isZphHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "往来", name: "isWlHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {label: '是否明细', name: 'isMx', width: 1, align: 'left', hidden: true},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                                {label: '简称', name: 'simpleName', width: 225, align: 'left', hidden: true}
                            ],
                            select: function (values, texts) {
                                kjkm = values;
                            }
                        });
                        page.search();
                    }
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                page.search(params);
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=kjcnzz', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');

                        var rowdatas = $('#gridtable').jfGridGet('rowdatas');
                        var item = {};
                        item.pzflList = rowdatas;
                        if (!alreadyformat) {
                            for (var i = 0; i < item.pzflList.length; i++) {
                                item.pzflList[i].jfs1 = $.formatMoney(item.pzflList[i].jfs1, 2);
                                item.pzflList[i].dfs1 = $.formatMoney(item.pzflList[i].dfs1, 2);
                                item.pzflList[i].ye1 = $.formatMoney(item.pzflList[i].ye1, 2);
                                item.pzflList[i].jfs2 = $.formatMoney(item.pzflList[i].jfs2, 2);
                                item.pzflList[i].dfs2 = $.formatMoney(item.pzflList[i].dfs2, 2);
                                item.pzflList[i].ye2 = $.formatMoney(item.pzflList[i].ye2, 2);
                            }
                        }
                        alreadyformat = true;
                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        // hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'reports/cnyeb/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'reports/cnyeb/pdf',
                    param: params
                });
            });
        },
        initGrid: function () {
            $("#gridtable").height($(window).height() - 170);
            $('#gridtable').jfGrid({
                url: config.base_server() + 'report2s/monthReport?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "名称", name: "kjkmName", width: (pageWidth * (18 / 100)), align: "left"},
                    {label: "月份", name: "kjqj", width: (pageWidth * (4 / 100)), align: "center"},
                    {
                        label: "月累计借方发生", name: "月累计借方发生", width: (pageWidth * (16 / 100)), align: 'center',
                        children: [
                            {
                                label: "出纳",
                                name: "jfs2",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "总账",
                                name: "jfs1",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "差额",
                                name: "jfs",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: "月累计贷方发生", name: "月累计贷方发生", width: (pageWidth * (16 / 100)), align: 'center',
                        children: [
                            {
                                label: "出纳",
                                name: "dfs2",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "总账",
                                name: "dfs1",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "差额",
                                name: "dfs",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: "期末余额", name: "期末余额", width: (pageWidth * (16 / 100)), align: 'center',
                        children: [
                            {
                                label: "出纳",
                                name: "ye2",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "总账",
                                name: "ye1",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "差额",
                                name: "ye",
                                width: (pageWidth * (8 / 100)),
                                align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    }
                ],
                reloadSelected: true,
                mainId: 'code'
            });
        },
        search: function (param) {
            param = param || {};

            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.kjqj = kjqj;

            params = param;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


